home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1843 / 1843.xpi / components / firebug-http-observer.js < prev    next >
Text File  |  2010-01-15  |  7KB  |  265 lines

  1. /* See license.txt for terms of usage */
  2.  
  3. // ************************************************************************************************
  4. // Constants
  5.  
  6. const CLASS_ID = Components.ID("{2D92593E-14D0-48ce-B260-A9881BBF9C8B}");
  7. const CLASS_NAME = "Firebug HTTP Observer Service";
  8. const CONTRACT_ID = "@joehewitt.com/firebug-http-observer;1";
  9.  
  10. const Cc = Components.classes;
  11. const Ci = Components.interfaces;
  12. const Cr = Components.results;
  13.  
  14. var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
  15. var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
  16.  
  17. // ************************************************************************************************
  18. // HTTP Request Observer implementation
  19.  
  20. var FBTrace = null;
  21. var fbs = null;
  22.  
  23. /**
  24.  * @service This service is intended as the only HTTP observer registered by Firebug.
  25.  * All FB extensions and Firebug itself should register a listener within this
  26.  * service in order to listen for http-on-modify-request, http-on-examine-response and
  27.  * http-on-examine-cached-response events.
  28.  *
  29.  * See also: <a href="http://developer.mozilla.org/en/Setting_HTTP_request_headers">
  30.  * Setting_HTTP_request_headers</a>
  31.  */
  32. function HttpRequestObserver()
  33. {
  34.     this.observers = [];
  35.     this.isObserving = false;
  36.  
  37.     // Get firebug-trace service for logging (the service should be already
  38.     // registered at this moment).
  39.     FBTrace = Cc["@joehewitt.com/firebug-trace-service;1"]
  40.        .getService(Ci.nsISupports).wrappedJSObject.getTracer("extensions.firebug");
  41.  
  42.     // Get firebug-service to listen for suspendFirebug and resumeFirebug events.
  43.     fbs = Cc["@joehewitt.com/firebug;1"].getService(Ci.nsISupports).wrappedJSObject;
  44.  
  45.     this.initialize();
  46. }
  47.  
  48. /* nsIFireBugClient */
  49. var FirebugClient =
  50. {
  51.     disableXULWindow: function()
  52.     {
  53.         if (gHttpObserverSingleton)
  54.             gHttpObserverSingleton.unregisterObservers();
  55.     },
  56.  
  57.     enableXULWindow: function()
  58.     {
  59.         if (gHttpObserverSingleton)
  60.             gHttpObserverSingleton.registerObservers();
  61.     }
  62. }
  63.  
  64. HttpRequestObserver.prototype =
  65. /** lends HttpRequestObserver */
  66. {
  67.     initialize: function()
  68.     {
  69.         fbs.registerClient(FirebugClient);
  70.  
  71.         observerService.addObserver(this, "quit-application", false);
  72.  
  73.         this.registerObservers();
  74.  
  75.     },
  76.  
  77.     shutdown: function()
  78.     {
  79.         fbs.unregisterClient(FirebugClient);
  80.  
  81.         observerService.removeObserver(this, "quit-application");
  82.  
  83.     },
  84.  
  85.     registerObservers: function()
  86.     {
  87.         if (this.isObserving)
  88.             return;
  89.  
  90.         observerService.addObserver(this, "http-on-modify-request", false);
  91.         observerService.addObserver(this, "http-on-examine-response", false);
  92.         observerService.addObserver(this, "http-on-examine-cached-response", false);
  93.  
  94.         this.isObserving = true;
  95.     },
  96.  
  97.     unregisterObservers: function()
  98.     {
  99.         if (!this.isObserving)
  100.             return;
  101.  
  102.         observerService.removeObserver(this, "http-on-modify-request");
  103.         observerService.removeObserver(this, "http-on-examine-response");
  104.         observerService.removeObserver(this, "http-on-examine-cached-response");
  105.  
  106.         this.isObserving = false;
  107.     },
  108.  
  109.     /* nsIObserve */
  110.     observe: function(subject, topic, data)
  111.     {
  112.         if (topic == "quit-application") {
  113.             this.shutdown();
  114.             return;
  115.         }
  116.  
  117.         try
  118.         {
  119.             if (topic == "http-on-modify-request" ||
  120.                 topic == "http-on-examine-response" ||
  121.                 topic == "http-on-examine-cached-response")
  122.             {
  123.                 this.notifyObservers(subject, topic, data);
  124.             }
  125.         }
  126.         catch (err)
  127.         {
  128.         }
  129.     },
  130.  
  131.     /* nsIObserverService */
  132.     addObserver: function(observer, topic, weak)
  133.     {
  134.         if (topic != "firebug-http-event")
  135.             throw Cr.NS_ERROR_INVALID_ARG;
  136.  
  137.         this.observers.push(observer);
  138.     },
  139.  
  140.     removeObserver: function(observer, topic)
  141.     {
  142.         if (topic != "firebug-http-event")
  143.             throw Cr.NS_ERROR_INVALID_ARG;
  144.  
  145.         for (var i=0; i<this.observers.length; i++) {
  146.             if (this.observers[i] == observer) {
  147.                 this.observers.splice(i, 1);
  148.                 return;
  149.             }
  150.         }
  151.  
  152.     },
  153.  
  154.     notifyObservers: function(subject, topic, data)
  155.     {
  156.         for (var i=0; i<this.observers.length; i++)
  157.             this.observers[i].observe(subject, topic, data);
  158.     },
  159.  
  160.     enumerateObservers: function(topic)
  161.     {
  162.         return null;
  163.     },
  164.  
  165.     /* nsISupports */
  166.     QueryInterface: function(iid)
  167.     {
  168.         if (iid.equals(Ci.nsISupports) ||
  169.             iid.equals(Ci.nsIObserverService) ||
  170.             iid.equals(Ci.nsIObserver)) {
  171.             return this;
  172.         }
  173.  
  174.         throw Cr.NS_ERROR_NO_INTERFACE;
  175.     }
  176. }
  177.  
  178. function safeGetName(request)
  179. {
  180.     try
  181.     {
  182.         return request.name;
  183.     }
  184.     catch (exc)
  185.     {
  186.         return null;
  187.     }
  188. }
  189.  
  190. // ************************************************************************************************
  191. // Service factory
  192.  
  193. var gHttpObserverSingleton = null;
  194. var HttpRequestObserverFactory =
  195. {
  196.     createInstance: function (outer, iid)
  197.     {
  198.         if (outer != null)
  199.             throw Cr.NS_ERROR_NO_AGGREGATION;
  200.  
  201.         if (iid.equals(Ci.nsISupports) ||
  202.             iid.equals(Ci.nsIObserverService) ||
  203.             iid.equals(Ci.nsIObserver))
  204.         {
  205.             if (!gHttpObserverSingleton)
  206.                 gHttpObserverSingleton = new HttpRequestObserver();
  207.             return gHttpObserverSingleton.QueryInterface(iid);
  208.         }
  209.  
  210.         throw Cr.NS_ERROR_NO_INTERFACE;
  211.     },
  212.  
  213.     QueryInterface: function(iid)
  214.     {
  215.         if (iid.equals(Ci.nsISupports) ||
  216.             iid.equals(Ci.nsISupportsWeakReference) ||
  217.             iid.equals(Ci.nsIFactory))
  218.             return this;
  219.  
  220.         throw Cr.NS_ERROR_NO_INTERFACE;
  221.     }
  222. };
  223.  
  224. // ************************************************************************************************
  225. // Module implementation
  226.  
  227. var HttpRequestObserverModule =
  228. {
  229.     registerSelf: function (compMgr, fileSpec, location, type)
  230.     {
  231.         compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
  232.         compMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME,
  233.             CONTRACT_ID, fileSpec, location, type);
  234.     },
  235.  
  236.     unregisterSelf: function(compMgr, fileSpec, location)
  237.     {
  238.         compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
  239.         compMgr.unregisterFactoryLocation(CLASS_ID, location);
  240.     },
  241.  
  242.     getClassObject: function (compMgr, cid, iid)
  243.     {
  244.         if (!iid.equals(Ci.nsIFactory))
  245.             throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  246.  
  247.         if (cid.equals(CLASS_ID))
  248.             return HttpRequestObserverFactory;
  249.  
  250.         throw Cr.NS_ERROR_NO_INTERFACE;
  251.     },
  252.  
  253.     canUnload: function(compMgr)
  254.     {
  255.         return true;
  256.     }
  257. };
  258.  
  259. // ************************************************************************************************
  260.  
  261. function NSGetModule(compMgr, fileSpec)
  262. {
  263.     return HttpRequestObserverModule;
  264. }
  265.